<html>
<head><meta charset="utf-8"><title>function pointers and address spaces · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html">function pointers and address spaces</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="167708879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167708879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167708879">(Jun 09 2019 at 17:25)</a>:</h4>
<p>Over in AVR-land, we recently hit <a href="https://github.com/avr-rust/rust/issues/143" target="_blank" title="https://github.com/avr-rust/rust/issues/143">an issue</a> about casting function pointers. It immediately affected us for printing function pointers with <code>Debug</code>, but it also occurs with futures-related stuff, such as this simplified Rust code:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">alpha</span><span class="p">(</span><span class="n">i</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">static</span><span class="w"> </span><span class="n">FOO</span>: <span class="nc">fn</span><span class="p">(</span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="n">alpha</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>This causes</p>
<div class="codehilite"><pre><span></span>Assertion failed: (CastInst::castIsValid(Instruction::BitCast, C, DstTy) &amp;&amp; &quot;Invalid constantexpr bitcast!&quot;), function getBitCast, file /Users/shep/Projects/avr-rust/src/llvm-project/llvm/lib/IR/Constants.cpp, line 1776.
</pre></div>


<p>Because the address spaces don't match:</p>
<div class="codehilite"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">SrcPtrTy</span><span class="o">-&gt;</span><span class="n">getAddressSpace</span><span class="p">()</span> <span class="o">!=</span> <span class="n">DstPtrTy</span><span class="o">-&gt;</span><span class="n">getAddressSpace</span><span class="p">())</span>
</pre></div>



<a name="167708928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167708928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167708928">(Jun 09 2019 at 17:26)</a>:</h4>
<p>This is the information we've gathered, but I'm not sure what to do next, or even what questions to ask next, so I figured I'd cast a wide net here and see if anyone had any suggestions.</p>



<a name="167744994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167744994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167744994">(Jun 10 2019 at 09:23)</a>:</h4>
<p>wow, so on AVR the address space for fn ptrs is different that for normal ptrs?</p>



<a name="167744997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167744997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167744997">(Jun 10 2019 at 09:23)</a>:</h4>
<p>embedded stuff is wild^^</p>



<a name="167767737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167767737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167767737">(Jun 10 2019 at 15:16)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> right, because AVR is Harvard architecture, not von Neumann (see also <a href="https://github.com/avr-rust/rust/issues/53" target="_blank" title="https://github.com/avr-rust/rust/issues/53">https://github.com/avr-rust/rust/issues/53</a>)</p>



<a name="167767801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167767801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167767801">(Jun 10 2019 at 15:16)</a>:</h4>
<p>So the code lives in a different address space "PROGMEM"</p>



<a name="167770694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167770694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167770694">(Jun 10 2019 at 15:51)</a>:</h4>
<p>looks like first and foremost a compiler issue to me</p>



<a name="167770702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167770702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167770702">(Jun 10 2019 at 15:51)</a>:</h4>
<p>but this also means you cannot cast fn ptrs to raw ptrs...</p>



<a name="167770726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167770726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167770726">(Jun 10 2019 at 15:52)</a>:</h4>
<p>rustc would have to set the "function" address space for <code>fn</code> types, I guess</p>



<a name="167772885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167772885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167772885">(Jun 10 2019 at 16:17)</a>:</h4>
<blockquote>
<p>you cannot cast fn ptrs to raw ptrs</p>
</blockquote>
<p>Right, LLVM dies. We had to <a href="https://github.com/avr-rust/rust/commit/17839c7c3c40188a94fac6a3955d7dc8f093e37c" target="_blank" title="https://github.com/avr-rust/rust/commit/17839c7c3c40188a94fac6a3955d7dc8f093e37c">add a hack for this</a> right now:</p>
<div class="codehilite"><pre><span></span><span class="n">fmt</span>::<span class="n">Pointer</span>::<span class="n">fmt</span><span class="p">(</span><span class="o">&amp;</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">()),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="c1">//                        ^^^^^^^^</span>
</pre></div>


<blockquote>
<p>rustc would have to set the "function" address space for fn types</p>
</blockquote>
<p>I think it does <em>sometimes</em>, which is why this is causing a problem. I think we need to find the piece of code that is responsible for casting function pointers and enhance it to preserve the address space for the source on the destination (maybe?)</p>



<a name="167772958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167772958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167772958">(Jun 10 2019 at 16:18)</a>:</h4>
<p>I'm not sure which compiler devs have experience in this area. I want to ping eddyb because I ping them for everything, but figured I'd wait a little while to see if anyone else has knowledge.</p>



<a name="167792168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167792168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167792168">(Jun 10 2019 at 20:11)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> might have an idea, or <span class="user-mention" data-user-id="133224">@Nikita Popov</span></p>



<a name="167809428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167809428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167809428">(Jun 11 2019 at 00:51)</a>:</h4>
<p>I have knowledge of address spaces but no idea why this specifically fails. it may be possible that all our statics assume data address space</p>



<a name="167809473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167809473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167809473">(Jun 11 2019 at 00:52)</a>:</h4>
<p>regardless of their type</p>



<a name="167809478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167809478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167809478">(Jun 11 2019 at 00:52)</a>:</h4>
<p>that being said I’m fairly confident our backend does not deal with address spaces at ALL</p>



<a name="167809505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167809505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167809505">(Jun 11 2019 at 00:53)</a>:</h4>
<p>I think I remember some murmur about somebody doing something in that direction but I haven’t heard anything about it since</p>



<a name="167842600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167842600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167842600">(Jun 11 2019 at 11:55)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> do you think there's a "small" part of the compiler you could point us to that deals with the address spaces of such things?</p>



<a name="167888598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167888598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167888598">(Jun 11 2019 at 20:43)</a>:</h4>
<p>From a quick search, we have <a href="https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/abi.rs#L365" target="_blank" title="https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/abi.rs#L365">https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/abi.rs#L365</a> and <a href="https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/type_.rs#L308" target="_blank" title="https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/type_.rs#L308">https://github.com/rust-lang/rust/blob/49d139c64b69ec5289f9f81db885ecfc2c7a8366/src/librustc_codegen_llvm/type_.rs#L308</a>.</p>



<a name="167889341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167889341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167889341">(Jun 11 2019 at 20:51)</a>:</h4>
<p>Possibly the code in <a href="https://github.com/rust-lang/rust/blob/da9ebc828c982d2ed49396886da85011e1b0a6c0/src/librustc_codegen_ssa/mir/rvalue.rs#L167" target="_blank" title="https://github.com/rust-lang/rust/blob/da9ebc828c982d2ed49396886da85011e1b0a6c0/src/librustc_codegen_ssa/mir/rvalue.rs#L167">https://github.com/rust-lang/rust/blob/da9ebc828c982d2ed49396886da85011e1b0a6c0/src/librustc_codegen_ssa/mir/rvalue.rs#L167</a> is relevant, it deals a lot with casting around function types.</p>



<a name="167955972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167955972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167955972">(Jun 12 2019 at 15:07)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> saw my Discord ramblings:</p>
<blockquote>
<p>the i8* is coming from us lowering miri allocations to LLVM</p>
<p>it wouldn't be that hard to change the addrspace when lowering a pointer to a function, as opposed to a data pointer (cc <span class="user-mention silent" data-user-id="124288">oli</span>)<br>
<a href="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322" target="_blank" title="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322">https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322</a><br>
so there's a cast here, where type_i8p shouldn't always be the same addrspace: <a href="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L331" target="_blank" title="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L331">https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L331</a></p>
<p>and then here, either check self.tcx.alloc_map.lock().get(alloc_id) for GlobalAlloc::Function or avoid the cast in scalar_to_backend by not passing in a type to scalar_to_backend (and so move the cast at the end of scalar_to_backend to its other 2 callers) <a href="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/consts.rs#L50" target="_blank" title="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/consts.rs#L50">https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/consts.rs#L50</a></p>
</blockquote>



<a name="167956238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167956238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167956238">(Jun 12 2019 at 15:10)</a>:</h4>
<p>Thank you <span class="user-mention" data-user-id="133224">@Nikita Popov</span> ! Now I have to try and act on some of this <span aria-label="innocent" class="emoji emoji-1f607" role="img" title="innocent">:innocent:</span></p>



<a name="167956504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167956504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167956504">(Jun 12 2019 at 15:13)</a>:</h4>
<p>ftr the hint was <code>i8*</code> in a <code>static</code></p>



<a name="167956704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167956704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167956704">(Jun 12 2019 at 15:14)</a>:</h4>
<p>yea, in <a href="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322" target="_blank" title="https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322">https://github.com/rust-lang/rust/blob/c4797fa4f4a696b183b3aa1517ee22c78d0f5d7a/src/librustc_codegen_llvm/common.rs#L322</a> we could just return early instead of falling through to the cast</p>



<a name="167958106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167958106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167958106">(Jun 12 2019 at 15:28)</a>:</h4>
<p>Y'all are the best. I'm gonna give this a try this evening</p>



<a name="167958114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167958114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167958114">(Jun 12 2019 at 15:28)</a>:</h4>
<p><span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="167963651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167963651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167963651">(Jun 12 2019 at 16:21)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> you can't not obey the cast</p>



<a name="167963735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/167963735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#167963735">(Jun 12 2019 at 16:22)</a>:</h4>
<p>it's just that for one caller, no cast is actually needed</p>



<a name="168002031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168002031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168002031">(Jun 13 2019 at 00:32)</a>:</h4>
<blockquote>
<p>either check <code>self.tcx.alloc_map.lock().get(alloc_id)</code> for <code>GlobalAlloc::Function</code></p>
</blockquote>
<p><span class="user-mention" data-user-id="119009">@eddyb</span>  an what would I do when I know it's a function? You mentioned:</p>
<blockquote>
<p>no cast is actually needed</p>
</blockquote>
<p>but I still need to do <em>some</em> cast, you said?</p>



<a name="168075466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168075466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168075466">(Jun 13 2019 at 19:03)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> no cast is needed when putting the pointer into a new allocation. but is needed for the other calls of that method</p>



<a name="168075505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168075505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168075505">(Jun 13 2019 at 19:04)</a>:</h4>
<p>like, one <code>scalar_to_backend</code> call doesn't need to do the cast. the one that always passes <code>i8p</code></p>



<a name="168076144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168076144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168076144">(Jun 13 2019 at 19:11)</a>:</h4>
<p>could put <code>Option</code> around that <code>&amp;'ll Type</code> argument, that'd be the simplest change I think?</p>



<a name="168077540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168077540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168077540">(Jun 13 2019 at 19:28)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> in <code>librustc_codegen_llvm</code> there's only one call to <code>scalar_to_backend</code> at all (there are other calls in <code>librustc_codegen_ssa</code> to a different function of the same name though)</p>



<a name="168077687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168077687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168077687">(Jun 13 2019 at 19:29)</a>:</h4>
<p>Or am I getting confused by traits</p>



<a name="168077922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168077922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168077922">(Jun 13 2019 at 19:32)</a>:</h4>
<p>Let's assume the latter. So the function is passed an <code>Option</code>; what do I do when it's <code>None</code>?</p>



<a name="168078100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168078100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168078100">(Jun 13 2019 at 19:34)</a>:</h4>
<p>Ultimately, this code needs to have the <code>llty</code>:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">llval</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">llvm</span>::<span class="n">LLVMConstInBoundsGEP</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">const_bitcast</span><span class="p">(</span><span class="n">base_addr</span><span class="p">,</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">type_i8p</span><span class="p">()),</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">const_usize</span><span class="p">(</span><span class="n">ptr</span><span class="p">.</span><span class="n">offset</span><span class="p">.</span><span class="n">bytes</span><span class="p">()),</span><span class="w"></span>
<span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="n">layout</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">layout</span>::<span class="n">Pointer</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">llvm</span>::<span class="n">LLVMConstPtrToInt</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">const_bitcast</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="168078880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168078880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168078880">(Jun 13 2019 at 19:44)</a>:</h4>
<p>For example, using your earlier comment about <code>GlobalAlloc::Function</code>, I <a href="https://github.com/shepmaster/rust/commit/2c9f43ac9aabfc565ce34e2b0ff1535a1f68a62d" target="_blank" title="https://github.com/shepmaster/rust/commit/2c9f43ac9aabfc565ce34e2b0ff1535a1f68a62d">had this scaffolding</a></p>



<a name="168079094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079094">(Jun 13 2019 at 19:46)</a>:</h4>
<p>Do you mean it's literally just <code>self.const_bitcast(base_addr, self.type_i8p()),</code> that isn't needed?</p>



<a name="168079119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079119">(Jun 13 2019 at 19:47)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> there is only one method with that name, and 3-4 calls to it</p>



<a name="168079132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079132">(Jun 13 2019 at 19:47)</a>:</h4>
<p>it's a trait, yes, and <code>rustc_codegen_ssa</code> code calls it</p>



<a name="168079157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079157">(Jun 13 2019 at 19:48)</a>:</h4>
<p>what I mean is literally just skip this:</p>
<div class="codehilite"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="n">layout</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">layout</span>::<span class="n">Pointer</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">llvm</span>::<span class="n">LLVMConstPtrToInt</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="n">const_bitcast</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="168079213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079213">(Jun 13 2019 at 19:48)</a>:</h4>
<p>yeah, I got confused by ripgrepping for <code>fn whatever</code> and seeing two "definitions" and forgetting that traits existed at all</p>



<a name="168079218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079218">(Jun 13 2019 at 19:48)</a>:</h4>
<p>that's a flexible cast (because LLVM's constants cast constructors are broken)</p>



<a name="168079245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079245">(Jun 13 2019 at 19:48)</a>:</h4>
<p>so instead of returning <code>cast(llval, llty)</code> you'd be returning just <code>llval</code></p>



<a name="168079263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079263">(Jun 13 2019 at 19:49)</a>:</h4>
<p>But the LLVM assertion is coming from <code>self.const_bitcast(base_addr, self.type_i8p()),</code></p>



<a name="168079278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079278">(Jun 13 2019 at 19:49)</a>:</h4>
<p>the lines before that if/else</p>



<a name="168079293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079293">(Jun 13 2019 at 19:49)</a>:</h4>
<p>it's <em>an assertion</em>?!</p>



<a name="168079299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079299">(Jun 13 2019 at 19:49)</a>:</h4>
<p>my bad</p>



<a name="168079322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079322">(Jun 13 2019 at 19:49)</a>:</h4>
<p>okay I see, there are two casts you'd need to skip</p>



<a name="168079382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079382">(Jun 13 2019 at 19:50)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> for <em>that</em> side, skip the cast &amp; GEPi when <code>ptr.offset.bytes()</code> is <code>0</code></p>



<a name="168079385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079385">(Jun 13 2019 at 19:50)</a>:</h4>
<p>since it's a noop then</p>



<a name="168079388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079388">(Jun 13 2019 at 19:50)</a>:</h4>
<p>Sorry, I thought I had pasted the original issue</p>



<a name="168079426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079426">(Jun 13 2019 at 19:50)</a>:</h4>
<p>nah I just can't read even if you did :P</p>



<a name="168079436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079436">(Jun 13 2019 at 19:50)</a>:</h4>
<p>anyway that will always be <code>0</code> for functions</p>



<a name="168079491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079491">(Jun 13 2019 at 19:51)</a>:</h4>
<p>and you still need the <code>Option</code> thing to avoid the next cast, I just didn't have the whole solution :P</p>



<a name="168079640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079640">(Jun 13 2019 at 19:53)</a>:</h4>
<p>And just return... <code>base_addr</code> ?</p>



<a name="168079834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079834">(Jun 13 2019 at 19:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> yupp!</p>



<a name="168079872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079872">(Jun 13 2019 at 19:56)</a>:</h4>
<p>that's the address of the function, in your case</p>



<a name="168079896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079896">(Jun 13 2019 at 19:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> it'd be easy to make it conditional changes of a <code>llval</code> variable, btw</p>



<a name="168079903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168079903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168079903">(Jun 13 2019 at 19:56)</a>:</h4>
<p>we do that in some places</p>



<a name="168080050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168080050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168080050">(Jun 13 2019 at 20:00)</a>:</h4>
<p>You mean like</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">llvar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="n">condition</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">llvar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">thing</span><span class="p">(</span><span class="n">llvar</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="168080124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168080124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168080124">(Jun 13 2019 at 20:02)</a>:</h4>
<p><code>llval</code> but yes :P</p>



<a name="168080132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168080132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168080132">(Jun 13 2019 at 20:02)</a>:</h4>
<p>Zulip is acting up wow</p>



<a name="168099698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168099698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168099698">(Jun 14 2019 at 01:38)</a>:</h4>
<p>Well I'll be. It compiled and my code compiled. Wonder if anything works.</p>



<a name="168184289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168184289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168184289">(Jun 15 2019 at 01:20)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> so, function pointers do appear to work on AVR, as to those stashed in a static; so thank you!</p>



<a name="168185205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185205">(Jun 15 2019 at 01:47)</a>:</h4>
<p>except that the optimizer might be ignoring all of the function pointers...</p>



<a name="168185276"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185276" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185276">(Jun 15 2019 at 01:49)</a>:</h4>
<p>And it is.</p>



<a name="168185280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185280">(Jun 15 2019 at 01:49)</a>:</h4>
<p>Any idea how to prevent that optimization?</p>



<a name="168185440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185440">(Jun 15 2019 at 01:53)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="w">    </span><span class="cp">#[inline(never)]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">gamma</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">SuperSerial</span><span class="p">.</span><span class="n">write_str</span><span class="p">(</span><span class="s">&quot;From gamma</span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">MY_FN</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">beta</span><span class="p">(</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">MY_FN</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">MY_FN</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">alpha</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>Seems to do the trick.</p>



<a name="168185483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185483">(Jun 15 2019 at 01:54)</a>:</h4>
<p>OK, now the next step is to figure out why async is totally broken</p>



<a name="168185484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185484">(Jun 15 2019 at 01:54)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> is this relying on <code>#[inline(never)]</code>?</p>



<a name="168185498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185498">(Jun 15 2019 at 01:55)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span> yes, it looks like at least one of the 3 is needed</p>



<a name="168185506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185506">(Jun 15 2019 at 01:55)</a>:</h4>
<p>To my knowledge, <code>#[inline(whatever)]</code> has no operational guarantees and is strictly a hint</p>



<a name="168185555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185555">(Jun 15 2019 at 01:57)</a>:</h4>
<p>Oh, I don't actually care about if this uses a function pointer in the real code or not, just that it's possible for the compiler in the current state to actually correctly codegen function pointers and use them</p>



<a name="168185561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185561">(Jun 15 2019 at 01:57)</a>:</h4>
<p>I think that the inline helps because of the splitting of the <code>Some</code> and the <code>if let</code></p>



<a name="168185564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185564">(Jun 15 2019 at 01:57)</a>:</h4>
<p>ah</p>



<a name="168185606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185606">(Jun 15 2019 at 01:58)</a>:</h4>
<p>So theoretically, I could do some strange thing like `write_volatile(...), if read_volatile(...) { val = Some(...) }</p>



<a name="168185608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185608">(Jun 15 2019 at 01:58)</a>:</h4>
<p>and assume that would be opaque enough for the optimizer</p>



<a name="168185624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168185624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168185624">(Jun 15 2019 at 01:59)</a>:</h4>
<p>anyway, off to bed, I'll spend another 10 minutes on this tomorrow <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="168259894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168259894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168259894">(Jun 16 2019 at 17:01)</a>:</h4>
<blockquote>
<p>except that the optimizer might be ignoring all of the function pointers...</p>
</blockquote>
<p>hmm? I don't understand what that means or why your hack might work</p>



<a name="168259895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168259895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168259895">(Jun 16 2019 at 17:01)</a>:</h4>
<p>are you exporting that <code>static</code>?</p>



<a name="168260503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260503">(Jun 16 2019 at 17:19)</a>:</h4>
<p>My test was basically (I forget exactly)</p>
<div class="codehilite"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">FOO</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="k">fn</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">whatever</span><span class="p">);</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FOO</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">f</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>And the optimizer said "nope, not actually an <code>Option</code> cause it's always there, nice try"</p>



<a name="168260554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260554">(Jun 16 2019 at 17:20)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> sorry but I don't know what you mean with regards to what the optimizer <em>did</em> :P</p>



<a name="168260565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260565">(Jun 16 2019 at 17:21)</a>:</h4>
<p>It never created a function pointer, it inlined <code>whatever</code> and removed the conditional from the <code>if let</code></p>



<a name="168260566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260566">(Jun 16 2019 at 17:21)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> also, yes, that <code>Option</code> is redundant, just like it would be in a <code>const</code></p>



<a name="168260613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260613">(Jun 16 2019 at 17:22)</a>:</h4>
<p>thus my "test" for does this compiler  handle function pointers never actually generated a function pointer</p>



<a name="168260623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260623">(Jun 16 2019 at 17:23)</a>:</h4>
<p><code>if let Some(f) = *black_box(&amp;FOO) { f() }</code> should do</p>



<a name="168260666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260666">(Jun 16 2019 at 17:24)</a>:</h4>
<p>(assuming you have access to that)</p>



<a name="168260670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260670">(Jun 16 2019 at 17:24)</a>:</h4>
<p>As I mentioned in passing, I got it to generate a function pointer by splitting the setting of the static and using the static in different (non-inlined) functions.</p>



<a name="168260673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260673">(Jun 16 2019 at 17:25)</a>:</h4>
<p>if this is for a test in rust-lang/rust, I'd prefer the direct approach</p>



<a name="168260682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260682">(Jun 16 2019 at 17:25)</a>:</h4>
<p>Test... hmm.</p>



<a name="168260684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260684">(Jun 16 2019 at 17:25)</a>:</h4>
<p>I thought this was a test?</p>



<a name="168260686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260686">(Jun 16 2019 at 17:25)</a>:</h4>
<p>I mean, it's me generating code and looking at the results of objdump</p>



<a name="168260726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260726">(Jun 16 2019 at 17:26)</a>:</h4>
<p>keep in mind the original source of this is a manual vtable, so <code>static FOO: fn(...)</code> or even <code>const FOO: &amp;fn(...)</code> is the closest to the original problem</p>



<a name="168260727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260727">(Jun 16 2019 at 17:26)</a>:</h4>
<p>I frankly have no idea how to actually write tests for this</p>



<a name="168260730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260730">(Jun 16 2019 at 17:26)</a>:</h4>
<p>I basically disassembled the function and looked for an <code>icall</code> instruction</p>



<a name="168260731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260731">(Jun 16 2019 at 17:26)</a>:</h4>
<p>yeah I guess run-pass is out of the question :/</p>



<a name="168260740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260740">(Jun 16 2019 at 17:27)</a>:</h4>
<p>heh, the test suite is slow enough on those beefy computers. It would be "omegalulz" for AVR</p>



<a name="168260792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260792">(Jun 16 2019 at 17:29)</a>:</h4>
<p>I'd be happy to try and write some kind of rust-lang/rust test, but I don't know how to trigger an <code>addrspace(1)</code> function to start with...</p>



<a name="168260795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260795">(Jun 16 2019 at 17:29)</a>:</h4>
<p>you could, I suppose, make a codegen test that passes --target... idk</p>



<a name="168260798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168260798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168260798">(Jun 16 2019 at 17:29)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> might</p>



<a name="168263459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168263459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168263459">(Jun 16 2019 at 18:54)</a>:</h4>
<p>yes you can write target-specific codegen tests now</p>



<a name="168363448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168363448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168363448">(Jun 18 2019 at 02:12)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> could you help me verify that this LLVM IR for a simple generator looks right? </p>
<div class="codehilite"><pre><span></span><span class="c">; ModuleID = &#39;blink.bpf1ew8c-cgu.0&#39;</span>
<span class="k">source_filename</span> <span class="p">=</span> <span class="s">&quot;blink.bpf1ew8c-cgu.0&quot;</span>
<span class="k">target</span> <span class="k">datalayout</span> <span class="p">=</span> <span class="s">&quot;e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8&quot;</span>
<span class="k">target</span> <span class="k">triple</span> <span class="p">=</span> <span class="s">&quot;avr-unknown-unknown&quot;</span>

<span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span> <span class="p">=</span> <span class="k">type</span> <span class="p">{</span> <span class="p">[</span><span class="m">0</span> <span class="k">x</span> <span class="k">i8</span><span class="p">],</span> <span class="k">i32</span><span class="p">,</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}</span>

<span class="vg">@0</span> <span class="p">=</span> <span class="k">private</span> <span class="k">unnamed_addr</span> <span class="k">constant</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">2</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}&gt;</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">2</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="k">c</span><span class="s">&quot;on&quot;</span> <span class="p">}&gt;,</span> <span class="k">align</span> <span class="m">1</span>
<span class="vg">@1</span> <span class="p">=</span> <span class="k">private</span> <span class="k">unnamed_addr</span> <span class="k">constant</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}&gt;</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="k">c</span><span class="s">&quot;off&quot;</span> <span class="p">}&gt;,</span> <span class="k">align</span> <span class="m">1</span>
<span class="vg">@2</span> <span class="p">=</span> <span class="k">private</span> <span class="k">unnamed_addr</span> <span class="k">constant</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">2</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}&gt;</span> <span class="p">&lt;{</span> <span class="p">[</span><span class="m">2</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="k">c</span><span class="s">&quot;\0D\0A&quot;</span> <span class="p">}&gt;,</span> <span class="k">align</span> <span class="m">1</span>


<span class="c">; blink::exercise_generator</span>
<span class="c">; Function Attrs: minsize noinline noreturn nounwind optsize uwtable</span>
<span class="k">define</span> <span class="k">internal</span> <span class="k">fastcc</span> <span class="kt">void</span> <span class="vg">@_ZN5blink18exercise_generator17h7a2821046021edbfE</span><span class="p">()</span> <span class="k">unnamed_addr</span> <span class="k">addrspace</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="vg">#5</span> <span class="k">personality</span> <span class="kt">void</span> <span class="p">()</span> <span class="k">addrspace</span><span class="p">(</span><span class="m">1</span><span class="p">)*</span> <span class="vg">@rust_eh_personality</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1319</span> <span class="p">{</span>
<span class="nl">bb2:</span>
  <span class="nv">%gen</span> <span class="p">=</span> <span class="k">alloca</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span>
  <span class="nv nv-Anonymous">%0</span> <span class="p">=</span> <span class="k">getelementptr</span> <span class="k">inbounds</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">,</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">*</span> <span class="nv">%gen</span><span class="p">,</span> <span class="k">i16</span> <span class="m">0</span><span class="p">,</span> <span class="k">i32</span> <span class="m">0</span><span class="p">,</span> <span class="k">i16</span> <span class="m">0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1348</span>
  <span class="nv nv-Anonymous">%1</span> <span class="p">=</span> <span class="k">bitcast</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">*</span> <span class="nv">%gen</span> <span class="k">to</span> <span class="k">i32</span><span class="p">*,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1349</span>
  <span class="k">store</span> <span class="k">i32</span> <span class="m">0</span><span class="p">,</span> <span class="k">i32</span><span class="p">*</span> <span class="nv nv-Anonymous">%1</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1349</span>
  <span class="nv">%.phi.trans.insert.i</span> <span class="p">=</span> <span class="k">getelementptr</span> <span class="k">inbounds</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">,</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">*</span> <span class="nv">%gen</span><span class="p">,</span> <span class="k">i16</span> <span class="m">0</span><span class="p">,</span> <span class="k">i32</span> <span class="m">0</span><span class="p">,</span> <span class="k">i16</span> <span class="m">5</span>
  <span class="nv">%.phi.trans.insert15.i</span> <span class="p">=</span> <span class="k">bitcast</span> <span class="k">i8</span><span class="p">*</span> <span class="nv">%.phi.trans.insert.i</span> <span class="k">to</span> <span class="k">i8</span><span class="p">**</span>
  <span class="nv nv-Anonymous">%2</span> <span class="p">=</span> <span class="k">getelementptr</span> <span class="k">inbounds</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">,</span> <span class="nv">%&quot;exercise_generator::{{closure}}&quot;</span><span class="p">*</span> <span class="nv">%gen</span><span class="p">,</span> <span class="k">i16</span> <span class="m">0</span><span class="p">,</span> <span class="k">i32</span> <span class="m">0</span><span class="p">,</span> <span class="k">i16</span> <span class="m">4</span>
  <span class="k">store</span> <span class="k">i8</span> <span class="m">1</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv nv-Anonymous">%2</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1370</span>
  <span class="k">store</span> <span class="k">i8</span><span class="p">*</span> <span class="nv nv-Anonymous">%2</span><span class="p">,</span> <span class="k">i8</span><span class="p">**</span> <span class="nv">%.phi.trans.insert15.i</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1371</span>
  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%bb3.i</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1373</span>

<span class="nl">bb3.i:</span>                                            <span class="c">; preds = %&quot;_ZN5blink18exercise_generator28_$u7b$$u7b$closure$u7d$$u7d$17hdda39e828092551eE.exit&quot;, %bb2</span>
<span class="c">; call blink::write_strln</span>
  <span class="k">call</span> <span class="k">fastcc</span> <span class="k">addrspace</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="kt">void</span> <span class="vg">@_ZN5blink11write_strln17h9d9f094c9a29470cE</span><span class="p">([</span><span class="m">0</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]*</span> <span class="k">noalias</span> <span class="k">nonnull</span> <span class="k">readonly</span> <span class="k">align</span> <span class="m">1</span> <span class="k">bitcast</span> <span class="p">(&lt;{</span> <span class="p">[</span><span class="m">2</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}&gt;*</span> <span class="vg">@0</span> <span class="k">to</span> <span class="p">[</span><span class="m">0</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]*),</span> <span class="k">i16</span> <span class="m">2</span><span class="p">),</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1374</span>
  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%&quot;_ZN5blink18exercise_generator28_$u7b$$u7b$closure$u7d$$u7d$17hdda39e828092551eE.exit&quot;</span>

<span class="nl">bb7.i:</span>                                            <span class="c">; preds = %&quot;_ZN5blink18exercise_generator28_$u7b$$u7b$closure$u7d$$u7d$17hdda39e828092551eE.exit&quot;</span>
<span class="c">; call blink::write_strln</span>
  <span class="k">call</span> <span class="k">fastcc</span> <span class="k">addrspace</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="kt">void</span> <span class="vg">@_ZN5blink11write_strln17h9d9f094c9a29470cE</span><span class="p">([</span><span class="m">0</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]*</span> <span class="k">noalias</span> <span class="k">nonnull</span> <span class="k">readonly</span> <span class="k">align</span> <span class="m">1</span> <span class="k">bitcast</span> <span class="p">(&lt;{</span> <span class="p">[</span><span class="m">3</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]</span> <span class="p">}&gt;*</span> <span class="vg">@1</span> <span class="k">to</span> <span class="p">[</span><span class="m">0</span> <span class="k">x</span> <span class="k">i8</span><span class="p">]*),</span> <span class="k">i16</span> <span class="m">3</span><span class="p">),</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1375</span>
  <span class="k">br</span> <span class="kt">label</span> <span class="nv">%&quot;_ZN5blink18exercise_generator28_$u7b$$u7b$closure$u7d$$u7d$17hdda39e828092551eE.exit&quot;</span>

<span class="nl">&quot;_ZN5blink18exercise_generator28_$u7b$$u7b$closure$u7d$$u7d$17hdda39e828092551eE.exit&quot;:</span> <span class="c">; preds = %bb3.i, %bb7.i</span>
  <span class="nv nv-Anonymous">%3</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i8</span><span class="p">*,</span> <span class="k">i8</span><span class="p">**</span> <span class="nv">%.phi.trans.insert15.i</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1376</span><span class="p">,</span> <span class="nv">!nonnull</span> <span class="nv nv-Anonymous">!14</span>
  <span class="nv nv-Anonymous">%4</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i8</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv nv-Anonymous">%3</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1376</span><span class="p">,</span> <span class="nv">!range</span> <span class="nv nv-Anonymous">!1377</span>
  <span class="nv nv-Anonymous">%5</span> <span class="p">=</span> <span class="k">xor</span> <span class="k">i8</span> <span class="nv nv-Anonymous">%4</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1378</span>
  <span class="k">store</span> <span class="k">i8</span> <span class="nv nv-Anonymous">%5</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv nv-Anonymous">%3</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1378</span>
  <span class="k">store</span> <span class="k">i32</span> <span class="m">3</span><span class="p">,</span> <span class="k">i32</span><span class="p">*</span> <span class="nv nv-Anonymous">%1</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1379</span>
  <span class="nv">%.pre.i</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i8</span><span class="p">*,</span> <span class="k">i8</span><span class="p">**</span> <span class="nv">%.phi.trans.insert15.i</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1380</span>
  <span class="nv">%.pre16.i</span> <span class="p">=</span> <span class="k">load</span> <span class="k">i8</span><span class="p">,</span> <span class="k">i8</span><span class="p">*</span> <span class="nv">%.pre.i</span><span class="p">,</span> <span class="k">align</span> <span class="m">1</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1380</span><span class="p">,</span> <span class="nv">!range</span> <span class="nv nv-Anonymous">!1377</span>
  <span class="nv">%phitmp.i</span> <span class="p">=</span> <span class="k">icmp</span> <span class="k">eq</span> <span class="k">i8</span> <span class="nv">%.pre16.i</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1381</span>
  <span class="k">br</span> <span class="k">i1</span> <span class="nv">%phitmp.i</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb7.i</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb3.i</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!1373</span>
<span class="p">}</span>
</pre></div>



<a name="168363482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168363482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168363482">(Jun 18 2019 at 02:14)</a>:</h4>
<p>It comes from <a href="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=ffa0fabc5bcbf2ab0f7a10e4e096ee2f" target="_blank" title="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=ffa0fabc5bcbf2ab0f7a10e4e096ee2f">this generator code</a>. I want to make sure that the <a href="https://github.com/avr-rust/rust/issues/143#issuecomment-501938644" target="_blank" title="https://github.com/avr-rust/rust/issues/143#issuecomment-501938644">change about casting pointers</a> you helped me with is unlikely to have any effect to this type of code.</p>



<a name="168403589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403589">(Jun 18 2019 at 14:01)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> generators don't need function pointers at all so none of this should've changed</p>



<a name="168403625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403625">(Jun 18 2019 at 14:01)</a>:</h4>
<p>You checked my implementation of the hack to make sure it wasn't overly broad, yeah?</p>



<a name="168403632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403632">(Jun 18 2019 at 14:01)</a>:</h4>
<p>I was afraid you'd say that though.</p>



<a name="168403696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403696">(Jun 18 2019 at 14:02)</a>:</h4>
<p>line-by-line assembly and LLVM-IR de-/re-construction is so tiresome</p>



<a name="168403741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403741">(Jun 18 2019 at 14:03)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> oh your change is not what I Was hoping for</p>



<a name="168403748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403748">(Jun 18 2019 at 14:03)</a>:</h4>
<p>Right.</p>



<a name="168403756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403756">(Jun 18 2019 at 14:03)</a>:</h4>
<p>It was the first version you suggested</p>



<a name="168403764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403764">(Jun 18 2019 at 14:03)</a>:</h4>
<p>I expected something very different</p>



<a name="168403767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403767">(Jun 18 2019 at 14:03)</a>:</h4>
<p>yeah okay</p>



<a name="168403768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403768">(Jun 18 2019 at 14:03)</a>:</h4>
<p>but then you went on the Option route</p>



<a name="168403779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403779">(Jun 18 2019 at 14:03)</a>:</h4>
<p>this original thing <em>could</em> break stuff</p>



<a name="168403789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403789">(Jun 18 2019 at 14:03)</a>:</h4>
<p>but I'm not sure</p>



<a name="168403841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403841">(Jun 18 2019 at 14:04)</a>:</h4>
<p>OK, so I should switch to the Option and pass in the none from the const allocation area</p>



<a name="168403909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168403909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168403909">(Jun 18 2019 at 14:05)</a>:</h4>
<p>yeah and <em>independently</em> of that, make the <code>ConstInBoundsGEP</code> stuff conditional on <code>ptr.offset.bytes() != 0</code> (or <code>ptr.offset != Size::ZERO</code>? not sure)</p>



<a name="168404023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168404023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168404023">(Jun 18 2019 at 14:06)</a>:</h4>
<p>Another roadblock I had was with this bit of code:</p>
<div class="codehilite"><pre><span></span><span class="w">            </span><span class="n">Scalar</span>::<span class="n">Raw</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">size</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u64</span><span class="p">,</span><span class="w"> </span><span class="n">layout</span><span class="p">.</span><span class="n">value</span><span class="p">.</span><span class="n">size</span><span class="p">(</span><span class="bp">self</span><span class="p">).</span><span class="n">bytes</span><span class="p">());</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">llval</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">const_uint_big</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">type_ix</span><span class="p">(</span><span class="n">bitsize</span><span class="p">),</span><span class="w"> </span><span class="n">data</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="n">layout</span><span class="p">.</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">layout</span>::<span class="n">Pointer</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">llvm</span>::<span class="n">LLVMConstIntToPtr</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="bp">self</span><span class="p">.</span><span class="n">const_bitcast</span><span class="p">(</span><span class="n">llval</span><span class="p">,</span><span class="w"> </span><span class="n">llty</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">},</span><span class="w"></span>
</pre></div>


<p>You suggested making <code>llty</code> the <code>Option</code>, so what should I do in that block?</p>



<a name="168404087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168404087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168404087">(Jun 18 2019 at 14:06)</a>:</h4>
<p>you have a <code>llval</code> <em>prior</em> to the cast</p>



<a name="168404101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168404101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168404101">(Jun 18 2019 at 14:06)</a>:</h4>
<p>which means you can just return that</p>



<a name="168404180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168404180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168404180">(Jun 18 2019 at 14:07)</a>:</h4>
<p>the <code>if</code>-<code>else</code> in there is just a cast. but weird because LLVM doesn't have a simple "cast this and figure out how" function for constant vals, despite having one for runtime casts</p>



<a name="168404289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168404289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168404289">(Jun 18 2019 at 14:08)</a>:</h4>
<p>this would only be tricky if you needed <code>llty</code> to create a value <em>at all</em>. but it's only used for a "type cast" (the bitwidth doesn't change)</p>



<a name="168406754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168406754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168406754">(Jun 18 2019 at 14:35)</a>:</h4>
<p>Does <a href="https://gist.github.com/shepmaster/af41bfc01f9a1427e57a56198e93d529" target="_blank" title="https://gist.github.com/shepmaster/af41bfc01f9a1427e57a56198e93d529">this diff</a> look closer to your intent?</p>



<a name="168406773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168406773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168406773">(Jun 18 2019 at 14:35)</a>:</h4>
<p>it seemingly compiles that code (now building everything else...)</p>



<a name="168409392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168409392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168409392">(Jun 18 2019 at 15:02)</a>:</h4>
<p>I would use <code>if let Some</code> and mutate inside, but this seems okay</p>



<a name="168409855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168409855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168409855">(Jun 18 2019 at 15:06)</a>:</h4>
<p>I can tweak that. This is a case where I really wish my RLS + emacs setup worked in the compiler tree. Figuring out what is what type is very slow.</p>



<a name="168409892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168409892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168409892">(Jun 18 2019 at 15:07)</a>:</h4>
<p>yeaaaaaaaah sadly I think it will take the death of save-analysis to make RLS viable for the compiler source</p>



<a name="168409966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168409966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168409966">(Jun 18 2019 at 15:08)</a>:</h4>
<p>you <em>could</em> ask me but that doesn't work when I'm not around :P</p>



<a name="168410017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168410017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168410017">(Jun 18 2019 at 15:08)</a>:</h4>
<p>one trick I rely on is double-click names so I can see all the places they occur</p>



<a name="168410031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168410031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168410031">(Jun 18 2019 at 15:08)</a>:</h4>
<p>but that doesn't get you that far sometimes</p>



<a name="168411832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/function%20pointers%20and%20address%20spaces/near/168411832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/function.20pointers.20and.20address.20spaces.html#168411832">(Jun 18 2019 at 15:25)</a>:</h4>
<p>Sadly, this change doesn't seem to make my generator work correctly.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>